<template>
  <view class="preview-container">
    <!-- 头部导航 -->
    <view class="header">
      <view class="back-icon" @click="goBack">
        <text>←</text>
      </view>
      <view class="header-title">修改预览</view>
    </view>
    
    <!-- 预览内容 -->
    <view class="preview-content">
      <view class="preview-section">
        <view class="section-title">房间信息</view>
        <view class="info-item">
          <text class="info-label">房间号</text>
          <text class="info-value">{{previewData.roomNumber || '201'}}</text>
        </view>
        <view class="info-item">
          <text class="info-label">房间类型</text>
          <text class="info-value">{{previewData.roomType}}</text>
        </view>
      </view>
      
      <view class="preview-section">
        <view class="section-title">时间信息</view>
        <view class="info-item">
          <text class="info-label">入住时间</text>
          <text class="info-value">{{previewData.checkInDate}}</text>
        </view>
        <view class="info-item">
          <text class="info-label">离店时间</text>
          <text class="info-value">{{previewData.checkOutDate}}</text>
        </view>
      </view>
      
      <view class="preview-section">
        <view class="section-title">联系信息</view>
        <view class="info-item">
          <text class="info-label">联系电话</text>
          <text class="info-value">{{previewData.phoneNumber}}</text>
        </view>
      </view>
      
      <view class="preview-section">
        <view class="section-title">住客信息</view>
        <view class="guest-list">
          <view class="guest-item" v-for="(guest, index) in previewData.guests" :key="index">
            <text class="guest-label">房间{{index + 1}}</text>
            <text class="guest-name">{{guest.name}}</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 底部按钮 -->
    <view class="button-group">
      <button class="cancel-btn" @click="goBack">取消</button>
      <button class="confirm-btn" @click="confirmModify">确认修改</button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';

// 预览数据
const previewData = ref({
  roomNumber: '',
  roomType: '',
  checkInDate: '',
  checkOutDate: '',
  phoneNumber: '',
  guests: []
});

// 从页面参数中获取数据
onMounted(() => {
  const eventChannel = getOpenerEventChannel();
  eventChannel.on('previewData', (data) => {
    if (data) {
      previewData.value = data;
    }
  });
});

// 返回按钮功能
const goBack = () => {
  uni.navigateBack();
};

// 确认修改
const confirmModify = () => {
  // 显示加载中
  uni.showLoading({
    title: '提交中...'
  });
  
  // 模拟网络请求
  setTimeout(() => {
    uni.hideLoading();
    
    // 返回数据给上一页
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    
    if (prevPage) {
      // 通过eventChannel返回数据
      const eventChannel = getOpenerEventChannel();
      eventChannel.emit('modifySuccess', previewData.value);
    }
    
    // 显示成功提示
    uni.showToast({
      title: '修改成功',
      icon: 'success',
      duration: 2000,
      success: () => {
        // 延迟返回，确保用户看到提示
        setTimeout(() => {
          uni.navigateBack();
        }, 2000);
      }
    });
  }, 1000);
};

// 获取事件通道
const getOpenerEventChannel = () => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const eventChannel = currentPage.getOpenerEventChannel();
  return eventChannel;
};
</script>

<style lang="scss">
.preview-container {
  background-color: #f5f5f5;
  min-height: 100vh;
  max-width: 750rpx;
  margin: 0 auto;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding-bottom: 40rpx;
}

// 头部导航
.header {
  position: relative;
  height: 90rpx;
  background: linear-gradient(to right, #1E90FF, #4682B4);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .back-icon {
    position: absolute;
    left: 30rpx;
    font-size: 40rpx;
    width: 60rpx;
    height: 60rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .header-title {
    font-size: 32rpx;
    font-weight: bold;
  }
}

// 预览内容
.preview-content {
  padding: 20rpx;
  
  .preview-section {
    background-color: #ffffff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
    
    .section-title {
      font-size: 30rpx;
      color: #333;
      font-weight: bold;
      margin-bottom: 20rpx;
      padding-left: 20rpx;
      border-left: 4rpx solid #1E90FF;
    }
    
    .info-item {
      display: flex;
      justify-content: space-between;
      padding: 15rpx 0;
      border-bottom: 1px solid #f5f5f5;
      
      &:last-child {
        border-bottom: none;
      }
      
      .info-label {
        color: #666;
        font-size: 28rpx;
      }
      
      .info-value {
        color: #333;
        font-size: 28rpx;
        font-weight: 500;
      }
    }
    
    .guest-list {
      .guest-item {
        display: flex;
        align-items: center;
        padding: 15rpx 0;
        border-bottom: 1px solid #f5f5f5;
        
        &:last-child {
          border-bottom: none;
        }
        
        .guest-label {
          color: #666;
          font-size: 28rpx;
          margin-right: 20rpx;
        }
        
        .guest-name {
          color: #333;
          font-size: 28rpx;
          font-weight: 500;
        }
      }
    }
  }
}

// 底部按钮
.button-group {
  padding: 40rpx 30rpx;
  display: flex;
  justify-content: space-between;
  
  button {
    width: 46%;
    height: 90rpx;
    border-radius: 45rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32rpx;
    font-weight: bold;
  }
  
  .cancel-btn {
    background-color: #f5f5f5;
    color: #666;
    border: 1px solid #e0e0e0;
  }
  
  .confirm-btn {
    background: linear-gradient(to right, #1E90FF, #4682B4);
    color: #ffffff;
    border: none;
  }
}

/* 移动端预览样式 */
@media screen and (min-width: 768px) {
  body {
    background-color: #f0f0f0;
  }
  
  uni-page-body {
    max-width: 375px !important;
    margin: 0 auto !important;
    position: relative !important;
    min-height: 100vh !important;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }
}
</style> 